﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SplitBinatyFile
{
    class FileSpliter
    {
        private string inFile;
        private string firstFile;
        private string secondFile;

        public FileSpliter(string inFile, string firstFile, string secondFile)
        {
            this.inFile = inFile;
            this.firstFile = firstFile;
            this.secondFile = secondFile;
        }

        public string InFile
        {
            get
            {
                return inFile;
            }
            set
            {
                inFile = value;
            }
        }

        public string FirstFile
        {
            get
            {
                return firstFile;
            }
            set
            {
                firstFile = value;
            }
        }

        public string SecondFile
        {
            get
            {
                return secondFile;
            }
            set
            {
                secondFile = value;
            }
        }
        public void SplitFile() 
        {
            BinaryReader reader = new BinaryReader(File.Open(this.InFile, FileMode.Open));
            BinaryWriter firstWriter = new BinaryWriter(File.Open(this.FirstFile,FileMode.Create));
            BinaryWriter secondWriter = new BinaryWriter(File.Open(this.SecondFile,FileMode.Create));

            using (reader) 
            {
                using (firstWriter) 
                {
                    using (secondWriter) 
                    {
                        long firstFileLenght = reader.BaseStream.Length / 2;

                        for (long i = 0; i < firstFileLenght; i++) 
                        {
                            byte nextByte = reader.ReadByte();
                            firstWriter.Write(nextByte);
                        }
                        for (long i = firstFileLenght; i < reader.BaseStream.Length; i++) 
                        {
                            byte nextByte = reader.ReadByte();
                            secondWriter.Write(nextByte);
                        }
                    }
                }
            }
        }

        public void MergeFiles() 
        {
            BinaryReader firstReader = new BinaryReader(File.Open(this.FirstFile, FileMode.Open));
            BinaryReader secondReader = new BinaryReader(File.Open(this.SecondFile, FileMode.Open));
            BinaryWriter writer = new BinaryWriter(File.Open("mergedInput.in", FileMode.Create));
            using (writer) 
            {
                using (firstReader) 
                {
                    for (long i = 0; i < firstReader.BaseStream.Length; i++) 
                    {
                        byte nextByte = firstReader.ReadByte();
                        writer.Write(nextByte);
                    }
                }
                using (secondReader) 
                {
                    for (long i = 0; i < secondReader.BaseStream.Length; i++) 
                    {
                        byte nextByte = secondReader.ReadByte();
                        writer.Write(nextByte);
                    }
                }
            }
        }
    }
}
